package cn.zjk.springframework.beans.factory;

import cn.zjk.springframework.beans.BeansException;
import cn.zjk.springframework.beans.PropertyValue;
import cn.zjk.springframework.beans.PropertyValues;
import cn.zjk.springframework.beans.factory.config.BeanDefinition;
import cn.zjk.springframework.beans.factory.config.BeanFactoryPostProcessor;
import cn.zjk.springframework.core.io.DefaultResourceLoader;
import cn.zjk.springframework.core.io.Resource;

import java.util.Properties;

/**
 * @description:
 * @Author zjk
 * @className: PropertyPlaceholderConfigurer
 * @date: 2023/4/4 13:17
 */
public class PropertyPlaceholderConfigurer implements BeanFactoryPostProcessor {
    /**
     * Default placeholder prefix: {@value}
     */
    public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
    /**
     * Default placeholder suffix: {@value}
     */
    public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";

    private String location;

    public void setLocation(String location) {
        this.location = location;
    }

    /**
     * 在Bean实例化之前，处理占位符
     * @param beanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        try {
            //加载属性文件
            DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
            Resource resource = resourceLoader.getResource(location);
            Properties properties = new Properties();
            properties.load(resource.getInputStream());

            String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
            for(String beanName:beanDefinitionNames){
                BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);

                PropertyValues propertyValues = beanDefinition.getPropertyValues();
                for(PropertyValue propertyValue: propertyValues.getPropertyValues()){
                    Object value = propertyValue.getValue();
                    if(!(value instanceof String)){
                        continue;
                    }
                    String strVal = (String) value;
                    StringBuilder stringBuilder = new StringBuilder();
                    int startIndex = strVal.indexOf(DEFAULT_PLACEHOLDER_PREFIX);
                    int stopIndex = strVal.indexOf(DEFAULT_PLACEHOLDER_SUFFIX);
                    if(startIndex!= -1 && stopIndex!=-1 && startIndex<stopIndex){
                        String proKey = strVal.substring(startIndex+2,stopIndex);
                        String proVal = properties.getProperty(proKey);
                        stringBuilder.replace(startIndex,stopIndex+1,proVal);
                        propertyValues.addPropertyValue(new PropertyValue(propertyValue.getName(),proVal));

                    }
                }
            }
        } catch (Exception e) {
            throw new BeansException("Could not load properties",e);
        }
    }
}
